/**
 * 
 */
package co.gov.medellin.sdep.sisdep.regulacion.service;

import java.util.Date;
import java.util.List;

import co.gov.medellin.sdep.sisdep.common.message.MessageDescriptorConstant;
import co.gov.medellin.sdep.sisdep.common.service.BaseService;
import co.gov.medellin.sdep.sisdep.model.Evento;
import co.gov.medellin.sdep.sisdep.model.dao.impl.IEventoDAOT;
import co.gov.medellin.sdep.sisdep.regulacion.exception.EventoException;
import co.gov.medellin.sdep.sisdep.regulacion.exception.EventoRuntimeException;
import co.gov.medellin.sdep.sisdep.regulacion.exception.descriptor.EventoMessageDescriptor;

/**
 * @author Usuario
 *
 */
public class EventoServiceImpl extends BaseService implements IEventoService {

	//----------------------------------------------------------------------
	//--- propiedades y constructores                                 init()
	//----------------------------------------------------------------------

	/**
	 * default serial version
	 */
	private static final long serialVersionUID = 1L;
	/** */
	private IEventoDAOT eventoDAO;

	/**
	 * 
	 */
	public EventoServiceImpl() {
	}

	//----------------------------------------------------------------------
	//--- propiedades y constructores                                  end()
	//----------------------------------------------------------------------

	//----------------------------------------------------------------------
	//--- implementacion de metodos para acciones                     init()
	//----------------------------------------------------------------------

	/**
	 * 
	 */
	public void insert(Evento evento) throws EventoException {
		try{
			evento.setUsuarioCrea(getPrincipalUser());
			evento.setFechaCrea(new Date());
			getEventoDAO().insert(evento);
		}catch (Exception e) {
			Object [] infoError = {
				evento.getId()
			};
			if(e.getCause().toString().indexOf("ORA-00001") >= 0){
				throw new EventoException(new EventoMessageDescriptor(MessageDescriptorConstant.UK_VIOLATION), infoError, e);
			}else{
				throw new EventoException(new EventoMessageDescriptor(MessageDescriptorConstant.SAVE), infoError, e);
			}
		}
	}

	/**
	 * 
	 */
	public final void update(Evento evento) throws EventoException {
		try{
			evento.setUsuarioModifica(getPrincipalUser());
			//evento.setFechaModifica(new Date());
			getEventoDAO().update(evento);
		}catch (Exception e) {
			Object [] infoError = {
				evento.getId()
			};
			if(e.getCause().toString().indexOf("ORA-00001") >= 0){
				throw new EventoRuntimeException(new EventoMessageDescriptor(MessageDescriptorConstant.UK_VIOLATION), infoError, e);
			}else{
				throw new EventoRuntimeException(new EventoMessageDescriptor(MessageDescriptorConstant.UPDATE), infoError, e);
			}
		}
	}

	/**
	 * 
	 */
	public final void delete(Evento evento) throws EventoException {
		try {
			evento.setUsuarioModifica(getPrincipalUser());
			evento.setFechaModifica(new Date());
			getEventoDAO().delete(evento);
		}catch (Exception e) {
			Object [] infoError = {
				evento.getId()
			};
			throw new EventoRuntimeException(new EventoMessageDescriptor(MessageDescriptorConstant.DELETE), infoError, e);
		}
	}

	/**
	 * @return 
	 * 
	 */
	public final List load(int start,int max, Evento evento) throws EventoException {
		try{
			//return getEventoDAO().getAllEventoPaginated(start, max, criteria);
			return getEventoDAO().load();
		}catch (Exception e) {
			Object [] infoError = null;
			throw new EventoRuntimeException(new EventoMessageDescriptor(MessageDescriptorConstant.RETRIEVE), infoError, e);
		}
	}

	/**
	 * @return 
	 * 
	 */
	public final List loadByRadicado(int start,int max, Evento evento) throws EventoException {
		try{
			//return getEventoDAO().getAllEventoPaginated(start, max, criteria);
			return getEventoDAO().loadByRadicado(evento);
		}catch (Exception e) {
			Object [] infoError = null;
			throw new EventoRuntimeException(new EventoMessageDescriptor(MessageDescriptorConstant.RETRIEVE), infoError, e);
		}
	}
	
	/**
	 * 
	 */
	public final Evento get(long id) throws EventoException {
		try{
			return getEventoDAO().findByPrimaryKey(id);
		}catch (Exception e) {
			Object [] infoError = {new Long(id)};
			throw new EventoRuntimeException(new EventoMessageDescriptor(MessageDescriptorConstant.GET), infoError, e);
		}
	}

	/**
	 * 
	 */
	public Evento getEventoRelatedBy(long idSolicitud)
			throws EventoException {
		try {
			Evento criteria = new Evento();
			criteria.setIdSolicitud(new Long(idSolicitud));
			return getEventoDAO().findByCriteria(criteria);
		} catch (Exception e) {
			Object[] infoError = { new Long(idSolicitud) };
			throw new EventoRuntimeException(
					new EventoMessageDescriptor(
							MessageDescriptorConstant.GET), infoError, e);
		}
	}

	//----------------------------------------------------------------------
	//--- implementacion de metodos para acciones                      end()
	//----------------------------------------------------------------------

	//----------------------------------------------------------------------
	//--- getters and setters                                         init()
	//----------------------------------------------------------------------

	/**
	 * @author Usuario
	 *
	 */
	public IEventoDAOT getEventoDAO() {
		return eventoDAO;
	}

	/**
	 * @author Usuario
	 *
	 */
	public void setEventoDAO(IEventoDAOT eventoDAO) {
		this.eventoDAO = eventoDAO;
	}

	//----------------------------------------------------------------------
	//--- getters and setters                                          end()
	//----------------------------------------------------------------------

}
